/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.IKeyListener;
import model.ErrorMessage;
import ui.List;
import ui.ListItem;

/**
 *
 * @author kien.tran
 */
public class ConfigForm extends AForm implements ICommandListener, IKeyListener {

    private List list;

    public ConfigForm() {
    }

    public ConfigForm(MStockMidlet control, Display display, int branch, int option) {
        super(control, display);
        this.branch = branch;
        this.option = option;
    }

    protected void initData() {
        lang = new String[]{"Tùy chọn", "Màn hình chính", "Cỡ chữ", "Giao diện", "Dữ liệu"};

        recycle = control.getRecycle();
        icon = control.getIcons()[7];
    }

    protected void initGui() {
        list = new List(display, control, lang[0], background, icon);
        ok = new Command("Chọn", Command.OK);
        back = new Command("Trở về", Command.BACK);
        list.setSelectCommand(ok);
        list.setCancelCommand(back);
        list.setCommandListener(this);
        list.setKeyListener(this);

        showData();
    }

    protected void loadData() {
    }

    protected void showData() {
        for (int i = 0; i < 4; i++) {
            this.list.addItem(new ListItem("-" + (i + 1), lang[i + 1], control.getIcons()[i + 11]));
        }
        this.list.show();
    }

    protected void showError(ErrorMessage error) {
    }

    public void commandAction(Command command) {
        if (command == ok) {
            int index = list.getSelectedIndex();

            if (index >= 0 && index < 5) {
                control.getBackManager().push(this.getClass().getName() + ":" + branch + ":" + option);

                switch (index) {
                    case 0:
                        new ConfigDataForm(control, display, branch, State.OTHER).show();
                        break;
                    case 1:
                        new ConfigDataForm(control, display, branch, State.SIZE).show();
                        break;

                    case 2:
                        new ConfigDataForm(control, display, branch, State.COLOR).show();
                        break;

                    case 3:
                        new ConfirmForm(control, display, branch, State.DATA).show();
                        break;
                }
            }
        } else if (command == back) {
            control.back(0);
        }
    }

    public void keyAction(int key) {
        if (key == List.FIRE) {
            commandAction(ok);
        }
    }
}
